// pages/mine/userinfo/index.js
var app = getApp();
import WxValidate from '../../../utils/WxValidate';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        aliyunOss: app.data.aliyunOss,
        flag: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var userInfo = app.globalData.userInfo;
        this.setData({
            userInfo: userInfo
        });

        var pwdQuestion = userInfo.pwdQuestion;
        if (pwdQuestion == undefined || Object.keys(pwdQuestion).length === 0) {
            this.setData({
                flag: false
            });
        }
        var pwdQuestionAnswer = userInfo.pwdQuestionAnswer;
        if (pwdQuestionAnswer == undefined || Object.keys(pwdQuestionAnswer).length === 0) {
            this.setData({
                flag: false
            });
        }
        var pwdQuestion2 = userInfo.pwdQuestion2;
        if (pwdQuestion2 == undefined || Object.keys(pwdQuestion2).length === 0) {
            this.setData({
                flag: false
            });
        }
        var pwdQuestionAnswer2 = userInfo.pwdQuestionAnswer2;
        if (pwdQuestionAnswer2 == undefined || Object.keys(pwdQuestionAnswer2).length === 0) {
            this.setData({
                flag: false
            });
        }
    },
    updatepass: function (events) {
        wx.navigateTo({
            url: 'repasssword/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    upimg: function () {
        var _this = this;

        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            success: function (res) {
                console.log(res)
                var tempFilePaths = res.tempFilePaths;
                wx.uploadFile({
                    url: app.data.service_url + '/app/uploadfile',
                    filePath: tempFilePaths[0],
                    name: 'file',
                    header: {
                        "Content-Type": "multipart/form-data"
                    },
                    success: function (res) {
                        var data = JSON.parse(res.data);
                        var code = data.code;
                        var arry = data.data;
                        if (code == "200") {
                            var userInfo = _this.data.userInfo;
                            var aliyunOss = _this.data.aliyunOss;
                            userInfo.avatar = aliyunOss + arry[0];
                            _this.setData({
                                userInfo: userInfo
                            })
                        } else {
                            var message = data.message;
                            wx.showToast({
                                title: message,
                                icon: 'none',
                                duration: 3000,
                                mask: false
                            })
                        }
                    },
                    fail: function (res) {
                        wx.showToast({
                            title: '请求失败',
                            icon: 'none',
                            duration: 3000,
                            mask: false
                        })
                    }
                })
            },
        })
    },
    inputvalue: function (e) {
        console.log(e)
        var name = e.currentTarget.dataset.name;
        var value = e.detail.value;
        var userInfo = this.data.userInfo;
        userInfo[name] = value;
        this.setData({
            userInfo: userInfo
        })
    },
    setPassword: function (e) {
        this.setData({
            password: e.detail.value
        })
    },
    showModal(error) {
        wx.showToast({
            title: error.msg,
            duration: 2000,
            icon: 'none'
        });
    },

    submitData: function (events) {
        this.setData({ buttonClicked: true })
        var userInfo = this.data.userInfo;
        const params = events.detail.value;
        console.log(params)
        if (params != undefined) {
            if (!this.WxValidate.checkForm(params)) {
                const error = this.WxValidate.errorList[0]
                this.showModal(error);
                return;
            }
        }
        app.paiRequest('/app/user/updateUserInfo', userInfo).then(function (res) {
            wx.switchTab({
                url: '/pages/index/index',
            })
        });
    },
    deleteRecord: function () {
        this.setData({
            flag: true
        })
    }
})